#!/bin/sh
#
# autogen.sh
#
# Copyright (C) 2009 - Higor Euripedes
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#

# +--------------------------------------------------------------------
# | CONFIGURATION.
# +--------------------------------------------------------------------

# whether to run ./configure on the end of the script
RUN_CONFIGURE=0
DEFAULT_CONFIGURE_ARGS=

# required extra programs
REQUIRE_PKG_CONFIG=1
REQUIRE_GETTEXTIZE=0

# set it to 1 if you are using autoconf2.13-style stuff.
USE_OLD_AUTOCONF=1

# subdirectories to enter and run autogen.sh
AUTOGEN_SUBDIRS="hacksqlc hacksqld hacksql"

# +--------------------------------------------------------------------
# | DO NOT EDIT THE LINES BELLOW.
# +--------------------------------------------------------------------

# functions 
echo_error() {
	if test "$TERM" = "xterm" -o "$TERM" = "rxvt" -o "$TERM" = "linux"; then
		printf "\033[31mError: \033[0m $@\n"
	else
		echo "Error: $@"
	fi
}

echo_warning() {
	if test "$TERM" = "xterm" -o "$TERM" = "rxvt" -o "$TERM" = "linux"; then
		printf "\033[33mWarning: \033[0m $@\n"
	else
		echo "Warning: $@"
	fi
}

clean_dir() {
	rm -rf autom4te.cache aclocal.m4 config.log \
		Makefile.in stamp-h* config.status config.h.in  \
		configure.tmp config.guess config.sub configure =
}

deep_clean() {
	rm -rf mkinstalldirs missing install-sh depcomp Makefile \
		src/Makefile config.h compile libtool ltmain.sh missing \
		config.rpath
	
	rm -rf `find . -iname '.deps'` `find . -iname '.libs'`
	rm -f  `find . -iname '*.o'`   `find . -iname '*~'`
	rm -f  `find . -iname 'Makefile.in'` 
}

check_prog() {
	if test "$1" != ""; then
		(exec $1 --version) 2>&1 > /dev/null
		if test $? -ne 0; then
			#echo "Error: This script requires $1 installed"
			echo_error "This script requires $1 installed"
			errors=$(($errors+1))
		fi
	fi
}

call_recursive() {
	if test $NO_RECURSE -eq 0; then
		if test ! -z "$AUTOGEN_SUBDIRS"; then
			for i in $AUTOGEN_SUBDIRS; do
				if test ! -d $i; then
					echo_warning "The subdirectory $i doesnt exist.. skipping"
				else
					echo "Entering $i and runing autogen.sh $1"
					if test "$i" != "configure"; then
						cd $i && ./autogen.sh $1 && cd ..
					else
						cd $i && ./autogen.sh && cd ..
					fi
					if test "$?" -ne 0; then
						echo_error "An error has happened while trying to run autogen.sh in $i"
						exit 1
					fi
				fi
			done
		fi
	fi
}

run_prog() {
	if test "$1" != ""; then
		(exec $@)
		if test $? -ne 0; then
			errors=$(($errors+1))
		fi
	fi
}

NO_RECURSE=0
if test "$i" = "norecurse"; then
	NO_RECURSE=1
fi

# clean files
if test "$1" = "clean"; then
	clean_dir
	deep_clean
	call_recursive clean
	exit 0
fi

echo "Checking for files..."

if test $USE_OLD_AUTOCONF -eq 1; then
	if test ! -f "configure.in"; then
		echo_error "Can't find configure.in on the current directory."
		exit 1
	fi
else
	if test ! -f "configure.ac"; then
		echo_error "Can't find configure.ac on the current directory."
		exit 1
	fi
fi

echo "Checking for programs..."
errors=0

# checks for the common programs

check_version (){
	version1=$2
	major1=`echo $version1 | cut -d. -f1`
	minor1=`echo $version1 | cut -d. -f2`
	
	version2=`$1 --version | grep $1 | awk '{print $4}' | awk -F. '{print $1"."$2}' | grep -v tuning`
	major2=`echo $version2 | cut -d. -f1`
	minor2=`echo $version2 | cut -d. -f2`
	
	if test $major2 -lt $major1; then
		echo_error "This script requires $1 version >= $2. $version2 is installed."
		exit 1
	else
		if test  $minor2 -lt $minor1; then
			echo_error "This script requires $1 version >= $2. $version2 is installed."
			exit 1
		fi
	fi
	check_version_result=$version2
}
#am_version=`automake --version | grep automake | awk '{print $4}' | awk -F. '{print $1"."$2}'`
#am_major=`automake --version | grep automake | awk '{print $4}' | awk -F. '{print $1}'`
#am_major=`automake --version | grep automake | awk '{print $4}' | awk -F. '{print $2}'`

check_prog autoconf
check_prog automake
check_prog autoheader
check_prog aclocal

echo "Checking for required versions of autoconf and automake..."
check_version autoconf "2.54"
check_version automake "1.4"

AM_OPTIONS=''
if test "$check_version_result" = "1.4"; then
	AM_OPTIONS=
fi

# if pkg-tool is required..
if test $REQUIRE_PKG_CONFIG -eq 1; then
	check_prog pkg-config
fi

# if gettextize is required..
if test $REQUIRE_GETTEXTIZE -eq 1; then
	check_prog gettextize
fi

if test $errors -ne 0; then
	echo
	echo "Some programs does not exist on your system, please install them"
	echo "and run this script again."
	echo
	exit 1
fi

# Recurse into subdirectories
call_recursive $1

echo "Runing programs..."
errors=0

clean_dir
touch NEWS README AUTHORS ChangeLog config.rpath ltmain.sh
run_prog aclocal
run_prog autoheader
run_prog automake --gnu --add-missing $AM_OPTIONS
run_prog autoconf

if test $errors -ne 0; then
	echo
	echo "$errors error happened, please read the messages above. Try to"
	echo "fix the errors and run this script again."
	echo
	exit 1
fi

# run configure on the end of the script
if test "$1" = "configure" -o $RUN_CONFIGURE -eq 1; then
	echo "Runing configure..."
	./configure $DEFAULT_CONFIGURE_ARGS
else
	echo
	echo "The configure script was created successfuly, you can now run"
	echo "./configure to set the build details."
fi

